package com.jl.jlapp.nets;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.jl.jlapp.mvp.base.MyApplication;

import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class Net {
    private static final GsonBuilder GSON_BUILDER = new GsonBuilder().serializeNulls().setDateFormat("yyyy-MM-dd").setPrettyPrinting();
    private static final long MAX_CACHE_SIZE = 10485760L;
    private static final OkHttpClient OK_HTTP_CLIENT = new OkHttpClient.Builder().cache(new Cache(new File(MyApplication.getSmartCacheDir(MyApplication.getInstance().getApplicationContext()), "retrofit"), 10485760L)).connectTimeout(30L, TimeUnit.SECONDS).readTimeout(30L, TimeUnit.SECONDS).writeTimeout(30L, TimeUnit.SECONDS).followRedirects(true).retryOnConnectionFailure(true).build();
    private static final int TIMEOUT = 30;
    private static Net instance;
    private Retrofit retrofit;
    private static List<Disposable> disposables;

    private static final String BASE_URL = "http://47.93.8.191:8042/JLMIS/";
    private static final String TEST_URL = "http://117.158.178.202:8001/JLMIS/";

    public Net() {
        if (this.retrofit == null) {
            HttpLoggingInterceptor localCommonInterceptor = new HttpLoggingInterceptor();
            localCommonInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            this.retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .client(new OkHttpClient.Builder().connectTimeout(10L, TimeUnit.SECONDS).readTimeout(20L, TimeUnit.SECONDS).writeTimeout(20L, TimeUnit.SECONDS).addInterceptor(localCommonInterceptor).retryOnConnectionFailure(true)
                            .build()).addConverterFactory(GsonConverterFactory.create()).addCallAdapterFactory(RxJava2CallAdapterFactory.createWithScheduler(Schedulers.io())).build();
        }
    }

    public Net(String paramString) {
        if (this.retrofit == null) {
            HttpLoggingInterceptor localCommonInterceptor = new HttpLoggingInterceptor();
            localCommonInterceptor.setLevel(HttpLoggingInterceptor.Level.NONE);
            this.retrofit = new Retrofit.Builder()
                    .baseUrl(paramString)
                    .client(new OkHttpClient.Builder().connectTimeout(10L, TimeUnit.SECONDS).addInterceptor(localCommonInterceptor).build())
                    .addConverterFactory(GsonConverterFactory.create()).addCallAdapterFactory(RxJava2CallAdapterFactory.createWithScheduler(Schedulers.io())).build();
        }
    }

    public static Api get() {
        if (instance == null) {
            synchronized (Net.class) {
                if (instance == null) {
                    instance = new Net();
                    disposables = new ArrayList<>();
                }
            }
        }
        return instance.service();
    }

    /* Error */
    public static Api get(String paramString) {
        if (instance == null) {
            synchronized (Net.class) {
                if (instance == null) {
                    instance = new Net(paramString);
                    disposables = new ArrayList<>();
                }
            }
        }
        return instance.service();
    }

    public static Gson getGson() {
        return GSON_BUILDER.create();
    }

    public Api service() {
        return (Api) this.retrofit.create(Api.class);
    }


    /**
     * 获取disposable 在onDestroy方法中取消订阅disposable.dispose()
     *
     * @param disposable disposable
     */
    public static void addDisposable(Disposable disposable) {
        if (disposables != null) {
            disposables.add(disposable);
        }
    }

    /**
     * 取消所有请求
     */
    public static void cancelAllRequest() {
        if (disposables != null) {
            for (Disposable disposable : disposables) {
                disposable.dispose();
            }
            disposables.clear();
        }
    }

    /**
     * 取消单个请求
     */
    public static void cancelSingleRequest(Disposable disposable) {
        if (disposable != null && !disposable.isDisposed()) {
            disposable.dispose();
        }
    }
}


/* Location:              /Users/davidzhang/Documents/decompileApk/jlfood-dex2jar.jar!/com/jl/jlapp/nets/Net.class
 * Java compiler version: 6 (50.0)
 * JD-Core Version:       0.7.1
 */